home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / prlbkxmp.lha / ch6 / suidscript < prev    next >
Text File  |  1991-01-08  |  2KB  |  74 lines

  1. #!/usr/bin/perl
  2.  
  3. # Usage: suidscript [dirlist]
  4.  
  5. # Make list of filenames to do find on.
  6.  
  7. if ($#ARGV >= 0) {
  8.     @list = @ARGV;
  9.     foreach $name (@ARGV) {
  10.     die "You must use absolute pathnames.\n"
  11.         unless $name =~ m|^/|;
  12.     }
  13. }
  14. else {
  15.     open(MT,"/etc/mount|") || die "Can't run /etc/mount: $!\n";
  16.  
  17.     while (<MT>) {
  18.     chop;
  19.     $_ .= <MT> if length($_) < 50;
  20.     @ary = split;
  21.     push(@list,$ary[2]) if ($ary[0] =~ m|^/dev|);
  22.     }
  23.     close MT;
  24.     $? && die "Couldn't run mount.\n";
  25. }
  26.  
  27. die "Can't find local filesystems" unless @list;
  28.  
  29. # Find all the set-id files.
  30.  
  31. open(FIND, "find @list -xdev -type f " .
  32.     "\\( -perm -04000 -o -perm -02000 \\) -print|");
  33.  
  34. while (<FIND>) {
  35.     chop;
  36.     next unless -T;                  # Not a text file.
  37.  
  38.     # Move script out of the way.
  39.  
  40.     print "Fixing ", $_, "\n";
  41.     ($dir,$file) = m#(.*)/(.*)#;
  42.     chdir $dir || die "Can't chdir to $dir: $!\n";
  43.     ($dev,$ino,$mode,$nlink,$uid,$gid) = stat($file);
  44.     die "Can't stat $_" unless $ino;
  45.     chmod $mode & 01777, $file;      # wipe out set[ug]id bits
  46.     rename($file,".$file");
  47.  
  48.     # Now write the wrapper.
  49.  
  50.     open(C,">.tmp$$.c") || die "Can't write C program for $_";
  51.     $real = "$dir/.$file";
  52.     print C <<EOW;
  53. main(argc,argv)
  54. int argc;
  55. char **argv;
  56. {
  57.     execv("$real",argv);
  58. }
  59. EOW
  60.     close C;
  61.  
  62.     # Now compile the wrapper.
  63.  
  64.     system '/bin/cc', ".tmp$$.c", '-o', $file;
  65.     die "Can't compile new $_" if $?;
  66.  
  67.     # Straighten out loose ends.
  68.  
  69.     chown $uid, $gid, $file;
  70.     chmod $mode, $file;
  71.     unlink ".tmp$$.c";
  72.     chdir '/';
  73. }
  74.